//
//  demo2.swift
//  foundationDemo
//
//  Created by Zhao Mingzhi on 16/6/21.
//  Copyright © 2016年 HNA. All rights reserved.
//

import Foundation
//获取沙盒路径
func getPath(){
    let dirHome:NSString = NSHomeDirectory()
    print(dirHome)
}
//获取document路径
func getDocument()->NSString{
    let document = NSSearchPathDirectory.DocumentDirectory
    let domain = NSSearchPathDomainMask.UserDomainMask
    let paths:NSArray = NSSearchPathForDirectoriesInDomains(document,domain,true)
    let documentsDirectory:NSString = paths.objectAtIndex(0) as! NSString
    print(documentsDirectory)
    return documentsDirectory
}
//获取Library路径
func getLibrary()->NSString{
    let Library = NSSearchPathDirectory.LibraryDirectory
    let domain = NSSearchPathDomainMask.UserDomainMask
    let paths:NSArray = NSSearchPathForDirectoriesInDomains(Library,domain,true)
    let LibraryDirectory:NSString = paths.objectAtIndex(0) as! NSString
    print(LibraryDirectory)
    return LibraryDirectory
}
//获取Cache路径
func getCache()->NSString{
    let Cache = NSSearchPathDirectory.CachesDirectory
    let domain = NSSearchPathDomainMask.UserDomainMask
    let paths:NSArray = NSSearchPathForDirectoriesInDomains(Cache,domain,true)
    let CacheDirectory:NSString = paths.objectAtIndex(0) as! NSString
    print(CacheDirectory)
    return CacheDirectory
}
//获取Tmp路径
func getTmp()->NSString{
    let paths = NSTemporaryDirectory()
    print(paths)
    return paths
}
//创建文件夹
func createDir(){
    let manager:NSFileManager = NSFileManager.defaultManager()
    let documentPath:NSString = getDocument()
    let testDirectory:NSString = documentPath.stringByAppendingPathComponent("test")
    do {
        let res = try manager.createDirectoryAtPath(testDirectory as String, withIntermediateDirectories:true, attributes: nil)
        print(res)
    }catch let error as NSError{
        print(error)
    }
}
//创建文件
func createFile(){
    let manager:NSFileManager = NSFileManager.defaultManager()
    let documentPath:NSString = getDocument()
    let testDirectory:NSString = documentPath.stringByAppendingPathComponent("test")
    let testPath:NSString = testDirectory.stringByAppendingPathComponent("test.txt")
    let res = manager.createFileAtPath(testPath as String, contents:nil, attributes: nil)
    print(res)

}
//文件写数据
func writeFile(){
    let documentPath:NSString = getDocument()
    let testDirectory:NSString = documentPath.stringByAppendingPathComponent("test")
    let testPath:NSString = testDirectory.stringByAppendingPathComponent("test.txt")
    let context:NSString = "写入内容"
    do {
        let res = try context.writeToFile(testPath as String, atomically: true, encoding: NSUTF8StringEncoding)
        print(res)
    }catch let error as NSError{
        print(error)
    }
}
//文件属性
func fileAttriutes(){
    let manager:NSFileManager = NSFileManager.defaultManager()
    let documentPath:NSString = getDocument()
    let testDirectory:NSString = documentPath.stringByAppendingPathComponent("test")
    let testPath:NSString = testDirectory.stringByAppendingPathComponent("test.txt")
    do {
        let fileAttributes:NSDictionary = try manager.attributesOfItemAtPath(testPath as String)
        let keys:NSArray;
        keys = fileAttributes.allKeys
        for i in 0 ..< keys.count {
            print("\(i) value\(keys[i])")
        }
        
    }catch let error as NSError{
        print(error)
    }
}
//文件删除
func deleteFile(){
    let manager:NSFileManager = NSFileManager.defaultManager()
    let documentPath:NSString = getDocument()
    let testDirectory:NSString = documentPath.stringByAppendingPathComponent("test")
    let testPath:NSString = testDirectory.stringByAppendingPathComponent("test.txt")
    do {
        let res = try manager.removeItemAtPath(testPath as String)
        print("文件删除成功")
        if(manager.isExecutableFileAtPath(testPath as String)){
            print("文件存在")
        }else{
            print("文件不存在")
        }
        
    }catch let error as NSError{
        print(error)
    }

    
}
